package exttable.testfiller.formcomponents;

import dbutils.DbUtils;
import exttable.testfiller.ValueVerifier;
import exttable.view.ViewTestComboBox;
import utils.Localisation;

import javax.swing.*;
import javax.swing.border.LineBorder;
import java.awt.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Admin
 * Date: Feb 20, 2010
 * Time: 2:06:14 PM
 * To change this template use File | Settings | File Templates.
 */
public class InputTextComponent implements IComponent {

    @Override
    public JTextField getInputTextComponent(int size, String name, String minValue, String maxValue, String validationType, String nameRu, String nameEn, String nameKz) {
        JTextField tf = new JTextField(size);
        tf.setName(name);
        tf.setInputVerifier(new ValueVerifier(minValue
                , maxValue
                , validationType.equals("VALIDATION")
                , nameRu, nameEn, nameKz,
                tf.getName()));

        return tf;
    }

    @Override
    public JTextField getInputTextComponent(int size, String name) {
        JTextField tf = new JTextField(size);
        tf.setName(name);
        return tf;
    }


    @Override
    public JComboBox getJComboBox(String tableName, String columnName) throws Exception {
        try {
            Connection s = DbUtils.getDBConnectionGeneral();
            PreparedStatement ps = s.prepareStatement("SELECT * FROM " + tableName);
            ResultSet rs = ps.executeQuery();
            List<ViewTestComboBox> list = new ArrayList<ViewTestComboBox>();
            JComboBox toReturn = new JComboBox();
            while (rs.next()) {
                //System.out.println("class  " + rs.getMetaData().getColumnTypeName(1));

                ViewTestComboBox ctc = new ViewTestComboBox.Build()
                        .id(rs.getString(1))
                        .nameRu(rs.getString(2)).build();
                list.add(ctc);
                toReturn = new JComboBox(list.toArray());

            }
            toReturn.setName(columnName);
            s.close();
            return toReturn;
        } catch (Exception e) {
            e.printStackTrace();
            throw new Exception("erro");
        }
    }

    @Override
    public JPanel getRadioPanel(String name) {
        final JPanel radioPanel = new JPanel(new GridLayout(1, 2, 3, 5));
        final JRadioButton rb1 = new JRadioButton(Localisation.getMessage("form.yes"));
        final JRadioButton rb2 = new JRadioButton(Localisation.getMessage("form.no"));
//        rb2.setSelected(true);
        ButtonGroup bg = new ButtonGroup();
        radioPanel.add(rb1);
        radioPanel.add(rb2);
        bg.add(rb1);
        bg.add(rb2);
        radioPanel.setName(name);
        return radioPanel;
    }

    @Override
    public JTextArea getJTextArea(String name) {
        JTextArea tf = new JTextArea(3, 5);
        tf.setName(name);
        tf.setBorder(LineBorder.createBlackLineBorder());
        return tf;
    }

    @Override
    public JCheckBox getCheckBox(String name) {
        JCheckBox cb = new JCheckBox();
        cb.setName(name);
        return cb;
    }

    @Override
    public JFormattedTextField getDateFormattedTextField(String name) {
        JFormattedTextField dateField = new JFormattedTextField(new SimpleDateFormat("dd-MM-yyyy"));
        dateField.setName(name);
        dateField.setValue(new Date());
        return dateField;
    }


}
